import threading
from Utils.MailSend import MailSend
from HTMLTestRunner import HTMLTestRunner
import unittest
import os
import time
from Case.TestCountInterface import TestCount
import configparser
path = os.getcwd()
# 读取全局配置
cfg = configparser.ConfigParser()

cfg.read(path + os.sep + "config.ini")

# 读取global全局配置
iftest = cfg.get("global","test_all")

# 读取开发和测试环境请求地址
dev_url = cfg.get("global","dev_host")
test_url = cfg.get("global","test_host")

# 读取通用采集地址url
bigdataxy_host = cfg.get("global","bigdataxy_host")

# 读取生成统一报告文件名称
report_file = cfg.get("global","report_file")

print("--------配置生效------")
print("系统识别：test_all为", iftest,"，系统将初始化测试",["线上" if iftest == "true" else "线下"],"环境！")

tests = unittest.defaultTestLoader.discover(path + os.sep + "Case",pattern="Test*.py")

fp = open(file=path + os.sep + "report" + os.sep + report_file,mode="w+",encoding="utf-8")

all_result = []

runner = HTMLTestRunner.HTMLTestRunner(
    title = "采集接口测试报告[" + time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) + "]",
    description="详细测试报告",
    stream = fp,
    verbosity=2
)

# 加载用例
tests1 = unittest.defaultTestLoader.loadTestsFromTestCase(TestCount)
# tests2 = unittest.defaultTestLoader.loadTestsFromTestCase(TestCount)

# 执行套件
def work():
    all_result.append(runner.run(tests1))

# # 执行套件
# def work1():
#     all_result.append(runner.run(tests2))


# 采用多线程执行用例并生成测试报告
t1 = threading.Thread(target=work)
# t2 = threading.Thread(target=work1)

t1.start()
# t2.start()
t1.join()
# t2.join()

print(all_result)

#执行结果发送邮件
MailSend(path + os.sep + "report" + os.sep + report_file)















